<template>
    <div class="components_edit_modal">
        <el-dialog width="800px" :title="title" :visible.sync="state" :show-close="true" :close-on-click-modal="false">
            <el-form ref="formInfo" :model="formInfo" :rules="formRules" label-width="95px" :inline="true" size="mini">
                <el-form-item label="商品名称：" prop="name">
                    <el-input v-model.trim="formInfo.name" clearable placeholder="请输入商品名称"></el-input>
                </el-form-item>

                <el-form-item label="商品类型：" prop="typeId">
                    <el-select v-model="formInfo.typeId" clearable filterable placeholder="请选择商品类型">
                        <el-option v-for="item in options.typeIdOptions" :key="item.id" :label="item.dictValue" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>

                <el-form-item label="商品价格：" prop="price">
                    <el-input-number v-model="formInfo.price" :min="0" :max="10000" label="请输入商品价格"></el-input-number>
                </el-form-item>

                <el-form-item label="商品序号：" prop="sort">
                    <el-input-number v-model="formInfo.sort" :min="0" :max="10000" label="请输入商品序号"></el-input-number>
                </el-form-item>

                <el-form-item label="热卖商品：" prop="isHot">
                    <el-switch v-model="formInfo.isHot">
                    </el-switch>
                </el-form-item>

                <el-form-item label="母婴用品：" prop="isMaternalInfant">
                    <el-switch v-model="formInfo.isMaternalInfant">
                    </el-switch>
                </el-form-item>

                <el-form-item label="最新上架：" prop="isNew">
                    <el-switch v-model="formInfo.isNew">
                    </el-switch>
                </el-form-item>

                <el-form-item label="特价商品：" prop="isSale">
                    <el-switch v-model="formInfo.isSale">
                    </el-switch>
                </el-form-item>

                <el-form-item label="金豆商品：" prop="onlyPoint">
                    <el-switch v-model="formInfo.onlyPoint">
                    </el-switch>
                </el-form-item>

                <el-form-item label="金豆价格：" prop="point">
                    <el-input-number v-model="formInfo.point" :min="0" :max="10000" label="请输入金豆价格"></el-input-number>
                </el-form-item>

                <el-form-item label="金豆抵扣：" prop="usePoint">
                    <el-switch v-model="formInfo.usePoint">
                    </el-switch>
                </el-form-item>

                <el-form-item label="封面上传：" prop="cover" class="inline-block">
                    <el-input v-model.trim="formInfo.cover" clearable style="display:none;"></el-input>
                    <pl-upload :postData="attachment" :api="'jstn-pic'" :displaymode="'images'" :imageOnly="false" :buttonIds='{select:"selectfiles1",upload:"postfiles1",container:"container1"}' @reloadFiles="reloadFiles"></pl-upload>
                </el-form-item>

                <el-form-item label="详情图片：" prop="images" class="inline-block">
                    <el-input v-model.trim="formInfo.cover" clearable style="display:none;"></el-input>
                    <pl-upload :postData="attachment1" :api="'jstn-pic'" :displaymode="'images'" :imageOnly="false" :buttonIds='{select:"selectfiles2",upload:"postfiles2",container:"container2"}' @reloadFiles="reloadFiles1"></pl-upload>
                </el-form-item>

                <el-form-item label="商品详情：" prop="description" class="inline-block">
                    <!-- 组件有两个属性 value 传入内容双向绑定 setting传入配置信息 --> 
                    <editor class="editor" 
                    :value="formInfo.description"  
                    :setting="editorSetting" 
                    @input="updateContent"></editor> 
                </el-form-item>

            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" size="mini" @click="addInfo('formInfo')">确 定</el-button>
                <el-button @click="state = false" size="mini">关 闭</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import cityInfo from '../../../cityInfo' //  城市列表
    import editor from '../../../common/editor'
    import { enums } from '@/constant'
    import { mapGetters } from 'vuex'
    import PlUpload from "../../../common/PlUpload"
    export default {
        components: {
            ...mapGetters({
                stat: 'getUserList_No',
            }),
            editor,
            "pl-upload": PlUpload
        },
        data() {
            return {
                state: false,
                //tinymce的配置信息 参考官方文档 https://www.tinymce.com/docs/configure/integration-and-setup/  
                editorSetting:{  
                    height:400,  
                },
                title: "",
                type: "",
                imageUrl: "",
                loading: false,
                attachment: [], //上传附件
                attachment1:[],
                value: "",
                formInfo: {
                    cover: '',//		string	
                    description: '请输入文本内容………………',//	string	富文本
                    isHot: true,//	是否热卖商品	boolean	
                    isMaternalInfant:true,//	是否是母婴用品	boolean	
                    isNew: true,//	是否最新上架	boolean	
                    isSale: true,//	是否特价商品	boolean	
                    name:'',		//string	
                    onlyPoint:true,//	是否是金豆商品	boolean	
                    point: 0,//	金豆		
                    price: 0,//		number	
                    shopId:null,//	所属店铺id		
                    sort: 0,//	序号	number	
                    typeId:null,//	类别id		
                    usePoint:true,//	是否使用金豆抵扣
                },
                formRules: {
                },
                cityarr: [],
                options: {
                    albumOptions: [],
                    topicOptions: [],
                    userOptions: []
                },
                props: {
                    value: 'name',
                    label: 'name',
                    children: 'sub'
                },
                attachment: [], //上传附件
            };
        },
        created() {
            this.getShopTypeList();
        },
        mounted() {
        },
        methods: {
            showDialog(val) {
                // console.log(val);
                if (this.$refs["formInfo"] !== undefined) {
                    this.$refs["formInfo"].resetFields('');
                }
                this.formInfo = {};
                if (val) {
                    for (var key in this.formInfo) {
                        this.formInfo[key] = val[key]?val[key]:true;
                    }
                    
                    
                        console.log('this.attachment1111111111')
                        console.log(this.formInfo)
                    if(this.formInfo.cover){
                        this.formInfo.bucket = this.urlFormat(this.formInfo.cover).bucket;
                        this.formInfo.object = this.urlFormat(this.formInfo.cover).objectKey;
                        this.formInfo.fileType = this.urlFormat(this.formInfo.cover).fileType;

                        if ( $.inArray(this.formInfo.fileType, ["jpg", "jpeg","image/jpeg", "gif", "png", "bmp"]) < 0 ) {
                            this.formInfo["isImage"] = false;
                            this.formInfo["imgsrc"] = "/static/js/plupload/suffix/otherfile.png";
                        }else{
                            this.formInfo["isImage"] = true;
                            this.formInfo["imgsrc"] = this.formInfo.cover;
                        }
                        
                        this.formInfo.fileRealName = this.formInfo.objectKey;
                        this.formInfo.imgsrc = this.formInfo.cover;
                        this.formInfo.url = this.formInfo.cover;
                        this.attachment.push(this.formInfo);

                    }
                    this.title = "编辑商品";
                    this.type = "edit";
                } else {
                    this.formInfo.description = '请输入文本内容………………';
                    this.title = "添加商品";
                    this.type = "add";
                }
                this.state = true;
                this.options.cityInfoOptions = cityInfo.cityInfo;
            },
            updateContent(data){//触发子组件城市选择-选择城市的事件
                this.formInfo.description = data;//改变了父组件的值  
            },
            // 选择城市
            handleChange(value) {
                if (value) {
                    this.formInfo.province = value[0];
                    this.formInfo.city = value[1];
                    this.formInfo.district = value[2];
                }
            },
            // 获取商品类型数据
            getShopTypeList() {
                let vm = this;
                vm.axios.get(api.dictionary.dictList + "?type=shop").then((response) => {
                    if (response.data.code == 200) {
                        console.log(response.data.data.dataList);
                        vm.options.typeIdOptions = response.data.data.dataList;
                    } else {
                        Message({
                            message: response.data.msg,
                            type: 'error'
                        });
                    }
                })
            },
            addInfo() {
                let vm = this;
                vm.$refs.formInfo.validate(valid => {
                    if (valid) {
                        let submission = ""; //提交方式
                        //选择提交方式
                        if (vm.type == "edit") {
                            submission = "put";
                        } else {
                            submission = "post";
                        }
                        // 提交
                        this.axios({
                            method: submission,
                            url: api.shopsManagement.shopsList,
                            data: vm.formInfo
                        }).then(response => {
                            if (response.data.code == 200) {
                                vm.state = false;
                                vm.$parent.$refs.tableList.$emit("vuetable:refresh");
                                vm.$message.success(response.data.message);
                            } else {
                                vm.$message.error(response.data.message);
                            }
                        });
                    } else {
                        return false;
                    }
                });
            },
            reloadFiles: function(files) {
                let vm = this;
                this.attachment = [];
                files.forEach((item, idnex) => {
                    item.url = item.fileStoreName;
                    this.attachment.push(item)
                })
                this.formInfo.cover = this.attachment[0].url;
            },
            reloadFiles1: function(files) {
                let vm = this;
                this.attachment1 = [];
                files.forEach((item, idnex) => {
                    item.url = item.fileStoreName;
                    this.attachment1.push(item)
                })
            },
            // 格式化 url 地址
            urlFormat(strUrl) {
                // var strUrl = "https://jstn-pic.oss-cn-qingdao.aliyuncs.com/d16e8272-4ac7-de6a-1071-a79ded59d29a.jpg"
                console.log(strUrl)
                var arrUrl = strUrl.split("//")[1].split("/");
                var objectKey = arrUrl[1];
                var bucket = arrUrl[0].split(".")[0];
                var fileType = arrUrl[1].split(".")[1];
                var resultObject = {
                    bucket: bucket,
                    objectKey: objectKey,
                    fileType: fileType
                }
                return resultObject
            }
        }
    };
</script>
<style scoped>

</style>